{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\h5py\\__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "from tensorflow.contrib.tensorboard.plugins import projector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting C:\\Users\\zdwxx\\Downloads\\Compressed\\MNIST_data\\train-images-idx3-ubyte.gz\n",
      "Extracting C:\\Users\\zdwxx\\Downloads\\Compressed\\MNIST_data\\train-labels-idx1-ubyte.gz\n",
      "Extracting C:\\Users\\zdwxx\\Downloads\\Compressed\\MNIST_data\\t10k-images-idx3-ubyte.gz\n",
      "Extracting C:\\Users\\zdwxx\\Downloads\\Compressed\\MNIST_data\\t10k-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From <ipython-input-2-bbab3714fffc>:81: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "\n",
      "Future major versions of TensorFlow will allow gradients to flow\n",
      "into the labels input on backprop by default.\n",
      "\n",
      "See tf.nn.softmax_cross_entropy_with_logits_v2.\n",
      "\n",
      "第 0 个周期 准确率是 0.0913\n",
      "第 550 个周期 准确率是 0.8161\n",
      "第 1100 个周期 准确率是 0.8317\n",
      "第 1650 个周期 准确率是 0.8383\n",
      "第 2200 个周期 准确率是 0.9278\n",
      "第 2750 个周期 准确率是 0.9331\n",
      "第 3300 个周期 准确率是 0.9383\n",
      "第 3850 个周期 准确率是 0.941\n",
      "第 4400 个周期 准确率是 0.9433\n",
      "第 4950 个周期 准确率是 0.9455\n",
      "第 5500 个周期 准确率是 0.9466\n",
      "第 6050 个周期 准确率是 0.9485\n",
      "第 6600 个周期 准确率是 0.9499\n",
      "第 7150 个周期 准确率是 0.9506\n",
      "第 7700 个周期 准确率是 0.953\n",
      "第 8250 个周期 准确率是 0.9539\n",
      "第 8800 个周期 准确率是 0.9557\n",
      "第 9350 个周期 准确率是 0.9562\n",
      "第 9900 个周期 准确率是 0.9569\n",
      "第 10450 个周期 准确率是 0.9575\n",
      "第 11000 个周期 准确率是 0.9589\n"
     ]
    }
   ],
   "source": [
    "# 载入数据集\n",
    "mnist = input_data.read_data_sets(r\"C:\\Users\\zdwxx\\Downloads\\Compressed\\MNIST_data\", one_hot=True)\n",
    "\n",
    "# 运行次数\n",
    "max_steps = 550 * 21\n",
    "\n",
    "# 图片数量\n",
    "image_num = 3000\n",
    "\n",
    "# 定义会话\n",
    "sess = tf.Session()\n",
    "\n",
    "# 文件路径\n",
    "DIR = \"C:/Tensorflow/\"\n",
    "\n",
    "# 载入图片\n",
    "embedding = tf.Variable(tf.stack(mnist.test.images[:image_num]), \n",
    "                        trainable=False, name=\"embedding\")\n",
    "\n",
    "# 定义一个参数概要\n",
    "def varible_summaries(var):\n",
    "    \n",
    "    with tf.name_scope(\"summary\"):\n",
    "        \n",
    "        mean = tf.reduce_mean(var)\n",
    "        tf.summary.scalar(\"mean\", mean) # 平均值\n",
    "        with tf.name_scope(\"stddev\"):\n",
    "            stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))\n",
    "        tf.summary.scalar(\"stddev\", stddev) # 标准差\n",
    "        tf.summary.scalar(\"max\", tf.reduce_max(var)) #最大值\n",
    "        tf.summary.scalar(\"min\", tf.reduce_min(var)) # 最小值\n",
    "        tf.summary.histogram(\"histogram\", var) # 直方图\n",
    "\n",
    "# 命名空间\n",
    "with tf.name_scope(\"input\"):\n",
    "    # 定义两个placeholder\n",
    "    x = tf.placeholder(tf.float32, [None, 784], name=\"x-input\")\n",
    "    y = tf.placeholder(tf.float32, [None, 10], name=\"y-input\")        \n",
    "\n",
    "# 显示图片\n",
    "with tf.name_scope(\"input_reshape\"):\n",
    "    \n",
    "    image_shaped_input = tf.reshape(x, [-1, 28, 28, 1])\n",
    "    tf.summary.image(\"input\", image_shaped_input, 10)\n",
    "    \n",
    "\n",
    "with tf.name_scope(\"layer\"):\n",
    "    #创建一个简单的神经网络\n",
    "    with tf.name_scope(\"wights1\"):\n",
    "        W1 = tf.Variable(tf.truncated_normal([784, 500], stddev=0.1), name=\"W1\")\n",
    "        varible_summaries(W1)\n",
    "        \n",
    "    with tf.name_scope(\"biases1\"):\n",
    "        b1 = tf.Variable(tf.zeros([500]) + 0.1, name=\"b1\")\n",
    "        varible_summaries(b1)\n",
    "        \n",
    "#     with tf.name_scope(\"wx_plus_b1\"):\n",
    "#         wx_plus_b1 = tf.matmul(x, W1) + b1\n",
    "    \n",
    "    with tf.name_scope(\"L1\"):\n",
    "        L1 = tf.nn.tanh(tf.matmul(x, W1) + b1)\n",
    "    \n",
    "    with tf.name_scope(\"wights2\"):\n",
    "        W2 = tf.Variable(tf.truncated_normal([500, 10], stddev=0.1), name=\"W2\")\n",
    "        varible_summaries(W2)\n",
    "        \n",
    "    with tf.name_scope(\"biases2\"):\n",
    "        b2 = tf.Variable(tf.zeros([10]) + 0.1, name=\"b2\")\n",
    "        varible_summaries(b2)\n",
    "        \n",
    "    with tf.name_scope(\"wx_plus_b2\"):\n",
    "        wx_plus_b2 = tf.matmul(L1, W2) + b2\n",
    "        \n",
    "    with tf.name_scope(\"softmax\"):\n",
    "        prediction = tf.nn.softmax(wx_plus_b2) # 预测值\n",
    "\n",
    "# 二次代价函数\n",
    "\n",
    "# loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))\n",
    "with tf.name_scope(\"loss\"):\n",
    "    loss = loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))\n",
    "    tf.summary.scalar(\"loss\", loss)\n",
    "    \n",
    "# 梯度下降法\n",
    "with tf.name_scope(\"train\"):\n",
    "    train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)\n",
    "\n",
    "# 初始化变量\n",
    "init = tf.global_variables_initializer()\n",
    "sess.run(init)\n",
    "\n",
    "with tf.name_scope(\"accuracy\"):\n",
    "    # 结果存放在一个布尔型列表中\n",
    "    with tf.name_scope(\"correct_prediction\"):\n",
    "        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(prediction, 1)) #argmax返回1维张量中最大的值所在的位置\n",
    "    # 求准确率\n",
    "    with tf.name_scope(\"accuracy\"):\n",
    "        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))#cast转换类型，True->1.0, False->0.0\n",
    "        tf.summary.scalar(\"accuracy\", accuracy)\n",
    "\n",
    "# 产生 metadata文件        \n",
    "if tf.gfile.Exists(DIR + \"projector/projector/metadata.tsv\"):\n",
    "    tf.gfile.DeleteRecursively(DIR + \"projector/projector/metadata.tsv\")\n",
    "\n",
    "with open(DIR + \"projector/projector/metadata.tsv\", \"w\") as f:\n",
    "    lables = sess.run(tf.argmax(mnist.test.labels[:], 1))\n",
    "    for i in range(image_num):\n",
    "        f.write(str(lables[i]) + \"\\n\")\n",
    "\n",
    "# 合并所有的summary\n",
    "merged = tf.summary.merge_all()\n",
    "\n",
    "\n",
    "projector_writer = tf.summary.FileWriter(DIR + \"projector/projector\", sess.graph)\n",
    "saver = tf.train.Saver()\n",
    "config = projector.ProjectorConfig()\n",
    "embed = config.embeddings.add()\n",
    "embed.tensor_name = embedding.name\n",
    "embed.metadata_path = DIR + \"projector/projector/metadata.tsv\"\n",
    "embed.sprite.image_path = DIR + \"projector/data/mnist_10k_sprite.png\"\n",
    "embed.sprite.single_image_dim.extend([28, 28])\n",
    "projector.visualize_embeddings(projector_writer, config)\n",
    "    \n",
    "\n",
    "for i in range(max_steps):\n",
    "\n",
    "    batch_xs, batch_ys = mnist.train.next_batch(100) #类似于read，一次读取100张图片\n",
    "    run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)\n",
    "    run_metadata = tf.RunMetadata()\n",
    "    summary = sess.run([train_step, merged], feed_dict={x : batch_xs, y : batch_ys})[1]\n",
    "    projector_writer.add_run_metadata(run_metadata, \"step%03d\" % i)\n",
    "    projector_writer.add_summary(summary, i)\n",
    "    \n",
    "    if i % 550 == 0:\n",
    "        acc = sess.run(accuracy, feed_dict={x:mnist.test.images, y:mnist.test.labels})\n",
    "        print(\"第\", i, \"个周期\", \"准确率是\", acc)\n",
    "\n",
    "saver.save(sess, DIR + \"projector/projector/a_model.ckpt\")\n",
    "projector_writer.close()\n",
    "sess.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
